package com.port.eleport.service.sys.impl;

import java.util.ArrayList;
import java.util.List;









import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.port.eleport.dao.BaseDaoI;
import com.port.eleport.dto.base.Tree;
import com.port.eleport.dto.sys.DictionaryType;
import com.port.eleport.model.sys.TDictionaryType;
import com.port.eleport.service.sys.DictionaryTypeServiceI;

@Service
public class DictionaryTypeServiceImpl implements DictionaryTypeServiceI {

	@Autowired
	private BaseDaoI<TDictionaryType> dictionaryTypeDao;

	
	public void add(DictionaryType r) {
		TDictionaryType t = new TDictionaryType();
		t.setName(r.getName());
		t.setSeq(r.getSeq());
		t.setDescription(r.getDescription());
		dictionaryTypeDao.save(t);
	}

	
	public void delete(Long id) {
		TDictionaryType t = dictionaryTypeDao.get(TDictionaryType.class, id);
		dictionaryTypeDao.delete(t);
	}

	
	public void edit(DictionaryType r) {
		TDictionaryType t = dictionaryTypeDao.get(TDictionaryType.class, r.getId());
		t.setDescription(r.getDescription());
		t.setName(r.getName());
		t.setSeq(r.getSeq());
		dictionaryTypeDao.update(t);
	}

	
	public DictionaryType get(Long id) {
		TDictionaryType t = dictionaryTypeDao.get(TDictionaryType.class, id);
		DictionaryType r = new DictionaryType();
		r.setDescription(t.getDescription());
		r.setId(t.getId());
		r.setName(t.getName());
		r.setSeq(t.getSeq());
		return r;
	}

	
	public List<Tree> tree() {
		List<TDictionaryType> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = dictionaryTypeDao.find("select distinct t from TDictionaryType t order by t.seq");

		if ((l != null) && (l.size() > 0)) {
			for (TDictionaryType r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getDictionaryType() != null) {
					tree.setPid(r.getDictionaryType().getId().toString());
					tree.setIconCls("icon_folder");
				}else{
					tree.setIconCls("icon_company");
				}
				tree.setText(r.getName());
				lt.add(tree);
			}
		}
		return lt;
	}





}
